﻿using System;
using System.Data.Objects;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace Metro.Kashikoi.Common.Entity
{
    /// <summary>
    /// 
    /// </summary>
    public sealed class ApplyDataContractResolverAttribute : Attribute, IOperationBehavior
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ApplyDataContractResolverAttribute"/> class.
        /// </summary>
        public ApplyDataContractResolverAttribute()
        {
        }

        /// <summary>
        /// Adds the binding parameters.
        /// </summary>
        /// <param name="description">The description.</param>
        /// <param name="parameters">The parameters.</param>
        public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
        {
        }

        /// <summary>
        /// Applies the client behavior.
        /// </summary>
        /// <param name="description">The description.</param>
        /// <param name="proxy">The proxy.</param>
        public void ApplyClientBehavior(OperationDescription description, System.ServiceModel.Dispatcher.ClientOperation proxy)
        {
            DataContractSerializerOperationBehavior dataContractSerializerOperationBehavior =
                description.Behaviors.Find<DataContractSerializerOperationBehavior>();
            dataContractSerializerOperationBehavior.DataContractResolver =
                new ProxyDataContractResolver();
        }

        /// <summary>
        /// Applies the dispatch behavior.
        /// </summary>
        /// <param name="description">The description.</param>
        /// <param name="dispatch">The dispatch.</param>
        public void ApplyDispatchBehavior(OperationDescription description, System.ServiceModel.Dispatcher.DispatchOperation dispatch)
        {
            DataContractSerializerOperationBehavior dataContractSerializerOperationBehavior =
                description.Behaviors.Find<DataContractSerializerOperationBehavior>();
            dataContractSerializerOperationBehavior.DataContractResolver =
                new ProxyDataContractResolver();
        }

        /// <summary>
        /// Validates the specified description.
        /// </summary>
        /// <param name="description">The description.</param>
        public void Validate(OperationDescription description)
        {
            // Do validation.
        }
    }
}
